home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-10-16 | 38.7 KB | 1,678 lines | [TEXT/KAHL] |
- /*
- File: ComponentTester.c
-
- Contains: Component tester routines.
-
- Written by: Gary Woodcock
-
- Copyright: © 1992 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- */
-
- //-----------------------------------------------------------------------
- // Includes
-
- #include "MathComponent.h"
- #include "MathComponentPrivate.h"
- #include "MoMathComponent.h"
- #include "MoMathComponentPrivate.h"
- #include "NuMathComponentPrivate.h"
- #include <Menus.h>
- #include <Windows.h>
- #include <QuickDraw.h>
- #include <OSEvents.h>
- #include <Resources.h>
- #include <Desk.h>
- #include <Fonts.h>
- #include <ToolUtils.h>
-
- #ifndef THINK_C
- #include <Packages.h>
- #endif THINK_C
-
- // Make sure the debug flag in this file is set appropriately
- #include "DebugFlags.h"
-
- //-----------------------------------------------------------------------
- // Constants
-
- // Maximum number of instances our app can open
- #define kMaxInstances 10
-
- // Menu bar
- enum
- {
- kMenuBarID = 128
- };
-
- // Menus
- enum
- {
- kAppleID = 128,
- kFileID,
- kEditID,
- kTestID,
- kCanDoID,
- kInstanceID
- };
-
- // Apple menu items
- enum
- {
- kAboutItem = 1
- };
-
- // File menu items
- enum
- {
- kOpenItem = 1,
- kCloseItem,
-
- #ifdef DEBUG_IT
- kOpenMathComponentItem = 4,
- kOpenMoMathComponentItem,
- kOpenNuMathComponentItem,
- kCloseComponentItem,
- kQuitItem = 9
- #else
- kOpenMoMathComponentItem = 4,
- kOpenNuMathComponentItem,
- kCloseComponentItem,
- kQuitItem = 8
- #endif DEBUG_IT
- };
-
- // Edit menu items
- enum
- {
- kUndoItem = 1,
- kCutItem = 3,
- kCopyItem,
- kPasteItem,
- kClearItem
- };
-
- // Test menu items
- enum
- {
- kGetNameItem = 1,
- kGetInfoItem,
- kGetVersionItem,
- kGetCountItem,
- kGetRefconItem,
- kGetA5Item,
- kCanDoItem = 8,
- kDoDivideItem = 10,
- kDoMultiplyItem,
- kDoAddItem
- };
-
- // Can Do hierarchical menu items
- enum
- {
- kCanDoOpenItem = 1,
- kCanDoCloseItem,
- kCanDoCanDoItem,
- kCanDoVersionItem,
- kCanDoRegisterItem,
- kCanDoTargetItem,
- kCanDoDoDivideItem = 8,
- kCanDoDoMultiplyItem,
- kCanDoDoAddItem
- };
-
- // Registration flags
- enum
- {
- kRegisterLocally = 0,
- kRegisterGlobally
- };
-
- // Dialog IDs
- enum
- {
- kAboutDialogID = 128,
- kStatusDialogID,
- kOpDialogID
- };
-
- // Common DITL items
- enum
- {
- kOKButton = 1,
- kCancelButton,
- kOKButtonOutline
- };
-
- // DITL items for operations dialog
- enum
- {
- kFirstNumBox = 4,
- kSecondNumBox
- };
-
- // DITL items for status dialog
- enum
- {
- kStatusBox = 1
- };
-
- // String list
- enum
- {
- kStatusStrsID = 128
- };
-
- // Strings
- enum
- {
- kIdleStr = 1,
- kFoundCompIDAndInstStr,
- kDidNotFindCompIDStr,
- kDidNotGetInstStr,
- kClosedOKStr,
- kErrorClosingInstStr,
- kCompVersStr,
- kInterfaceVersStr,
- kCanDoOpenStr,
- kCantDoOpenStr,
- kCanDoCloseStr,
- kCantDoCloseStr,
- kCanDoCanDoStr,
- kCantDoCanDoStr,
- kCanDoVersionStr,
- kCantDoVersionStr,
- kCanDoRegisterStr,
- kCantDoRegisterStr,
- kCanDoTargetStr,
- kCantDoTargetStr,
- kNameStr,
- kNoNameStr,
- kInfoStr,
- kNoInfoStr,
- kRefconStr,
- kThereIsStr,
- kThereAreStr,
- kInstanceStr,
- kInstancesStr,
- kA5Str,
- kDivideResultStr,
- kCanDoDoDivideStr,
- kCantDoDoDivideStr,
- kMultiplyResultStr,
- kCanDoDoMultiplyStr,
- kCantDoDoMultiplyStr,
- kAddResultStr,
- kCanDoDoAddStr,
- kCantDoDoAddStr,
- kCantOpenAnyMoreInstStr,
- kCompDescStr,
- kMathStr,
- kMoMathStr,
- kNuMathStr,
- kKeyPhrase
- };
-
- // Math component variants
- typedef enum MathType
- {
- kMathComponent = 1,
- kMoMathComponent,
- kNuMathComponent
- }
- MathType;
-
- //-----------------------------------------------------------------------
- // Globals
-
- MenuHandle gAppleMenu;
- MenuHandle gFileMenu;
- MenuHandle gEditMenu;
- MenuHandle gTestMenu;
- MenuHandle gCanDoMenu;
- MenuHandle gInstanceMenu;
- EventRecord gTheEvent;
- Component gMathComponentID;
- Component gMoMathComponentID;
- Component gNuMathComponentID;
- ComponentInstance gComponentInstance [kMaxInstances];
- ComponentInstance gCurrentInstance;
- DialogPtr gStatusDialog;
- GrafPtr gSavedPort;
- Str255 gStatusStr;
- Rect gStatusBox;
- Handle gStatusText;
- Handle gMathNameHdl;
- Handle gMoMathNameHdl;
- Handle gNuMathNameHdl;
- Handle gInfoHdl;
- Str255 gKeyPhrase;
- Boolean gQuitFlag;
-
- //-----------------------------------------------------------------------
- // Prototypes
-
- void DoInit (void);
- void DoMenuSetup (void);
- void HandleEvent (void);
- void HandleMouseDown (void);
- void AdjustMenus (void);
- void Enable (Handle menu,
- short item,
- Boolean ok);
- void HandleMenu (long menu);
- void OpenStatus (void);
- void UpdateStatus (void);
- void DoAboutDialog (void);
- void OpenMathComponent (MathType whichOne);
- void DoOperationDialog (short *numerator,
- short *denominator);
- static pascal void DrawOKOutline (DialogPtr theDialog,
- short theItem);
- void DoQuit (void);
- void DecimalNumberToHexStr (long theDecimalNumber,
- StringPtr theHexStr);
- OSErr HexStrToDecimalNumber (StringPtr theHexStr,
- long *theDecimalNumber);
-
- //-----------------------------------------------------------------------
-
- main (void)
- {
- // Init
- DoInit();
- DoMenuSetup();
- OpenStatus();
-
- // Eat events until done
- do
- {
- HandleEvent();
- }
- while (!gQuitFlag);
-
- // Take off, eh?
- ExitToShell();
- }
-
- //-----------------------------------------------------------------------
-
- void DoInit (void)
- {
- short i;
- ComponentDescription mathDesc;
-
- // Set up quit flag
- gQuitFlag = false;
-
- // MacMantra™
- MaxApplZone();
- InitGraf (&qd.thePort);
- InitFonts();
- FlushEvents (everyEvent, 0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs (0L);
- InitCursor();
- MoreMasters();
- MoreMasters();
- MoreMasters();
- MoreMasters();
-
- // Get initial strings
- GetIndString (gStatusStr, kStatusStrsID, kIdleStr);
- GetIndString (gKeyPhrase, kStatusStrsID, kKeyPhrase);
-
- // Init instance stuff
- gMathComponentID = 0L;
- gMoMathComponentID = 0L;
- gNuMathComponentID = 0L;
- gCurrentInstance = 0L;
- for (i = 0; i < kMaxInstances; i++)
- gComponentInstance[i] = 0L;
-
- // Register (or find) our components
- mathDesc.componentType = mathComponentType;
- mathDesc.componentSubType = 0L;
- mathDesc.componentManufacturer = 'appl';
- mathDesc.componentFlags = 0L;
- mathDesc.componentFlagsMask = 0L;
-
- // Are we debugging?
- #ifdef DEBUG_IT
-
- if (gMathNameHdl == nil)
- {
- Str255 mathName = "\pMath Component";
-
- gMathNameHdl = NewHandle (sizeof (Str255));
- BlockMove ((Ptr)&mathName, (Ptr)(*gMathNameHdl), mathName[0] + 1);
- }
- if (gMoMathNameHdl == nil)
- {
- Str255 moMathName = "\pMoMath Component";
-
- gMoMathNameHdl = NewHandle (sizeof (Str255));
- BlockMove ((Ptr)&moMathName, (Ptr)(*gMoMathNameHdl), moMathName[0] + 1);
- }
- if (gNuMathNameHdl == nil)
- {
- Str255 nuMathName = "\pNuMath Component";
-
- gNuMathNameHdl = NewHandle (sizeof (Str255));
- BlockMove ((Ptr)&nuMathName, (Ptr)(*gNuMathNameHdl), nuMathName[0] + 1);
- }
- if (gInfoHdl == nil)
- {
- Str255 mathInfo = "\pThis component provides simple math services.";
-
- gInfoHdl = NewHandle (sizeof (Str255));
- BlockMove ((Ptr)&mathInfo, (Ptr)(*gInfoHdl), mathInfo[0] + 1);
- }
-
- // Register the Math component
- gMathComponentID = RegisterComponent (&mathDesc,
- (ComponentRoutine) MathDispatcher, kRegisterLocally,
- gMathNameHdl, gInfoHdl, nil);
-
- // Register the NuMath component
- mathDesc.componentFlags = 0x80000000;
- gNuMathComponentID = RegisterComponent (&mathDesc,
- (ComponentRoutine) NuMathDispatcher, kRegisterLocally,
- gNuMathNameHdl, gInfoHdl, nil);
-
- // Register the MoMath component
- mathDesc.componentManufacturer = moMathComponentMfg;
- mathDesc.componentFlags = 0L;
- gMoMathComponentID = RegisterComponent (&mathDesc,
- (ComponentRoutine) MoMathDispatcher, kRegisterLocally,
- gMoMathNameHdl, gInfoHdl, nil);
-
- #else // We're running as standalone components
-
- // Find the NuMath component - Note that we won't be able to find the
- // Math component, because the NuMath component captured it. In essence,
- // the NuMath component completely replaces the Math component (from an
- // application point of view). Also, you should note that it is perfectly
- // acceptable to do a RegisterComponent call followed by a FindNextComponent
- // call - if you use the same component description, and the
- // RegisterComponent call was successful, then you'll get back the same
- // component ID from FindNextComponent that you got from RegisterComponent.
- gNuMathComponentID = FindNextComponent (nil, &mathDesc);
-
- // Find the MoMath component
- mathDesc.componentManufacturer = moMathComponentMfg;
- gMoMathComponentID = FindNextComponent (nil, &mathDesc);
-
- #endif DEBUG_IT
- }
-
- //-----------------------------------------------------------------------
-
- void DoMenuSetup (void)
- {
- Handle theMenuBar = GetNewMBar (kMenuBarID);
-
- // Set up our menus
- SetMenuBar (theMenuBar);
- gAppleMenu = GetMHandle (kAppleID);
- gFileMenu = GetMHandle (kFileID);
-
- // We don't need the "Open Math Component" item unless
- // we're debugging
- #ifndef DEBUG_IT
- DelMenuItem (gFileMenu, 4);
- #endif DEBUG_IT
-
- gEditMenu = GetMHandle (kEditID);
- gTestMenu = GetMHandle (kTestID);
- gInstanceMenu = GetMHandle (kInstanceID);
- gCanDoMenu = GetMenu (kCanDoID);
- InsertMenu (gCanDoMenu, hierMenu);
- AddResMenu (gAppleMenu, 'DRVR');
- DrawMenuBar();
- }
-
- //-----------------------------------------------------------------------
-
- void HandleEvent (void)
- {
- Boolean ok = false;
-
- // Do system stuff
- HiliteMenu (0);
- SystemTask();
-
- // Suck an event
- ok = WaitNextEvent (everyEvent, &gTheEvent, 0, 0);
- if (ok)
- {
- // What was it?
- switch (gTheEvent.what)
- {
- case mouseDown:
- {
- HandleMouseDown();
- break;
- }
- case keyDown:
- case autoKey:
- {
- char theChar = gTheEvent.message & charCodeMask;
- long theMenu = MenuKey (theChar);
-
- HandleMenu (theMenu);
- break;
- }
- case updateEvt:
- {
- if (IsDialogEvent (&gTheEvent))
- {
- // Update the status dialog
- GetPort (&gSavedPort);
- SetPort (gStatusDialog);
- BeginUpdate (gStatusDialog);
- InsetRect (&gStatusBox, -2, -2);
- FrameRect (&gStatusBox);
- InsetRect (&gStatusBox, 2, 2);
- EraseRect (&gStatusBox);
- SetIText (gStatusText, gStatusStr);
- EndUpdate (gStatusDialog);
- SetPort (gSavedPort);
- }
- }
- default: // We don't really care about any other events
- {
- break;
- }
- }
- }
- }
-
- //-----------------------------------------------------------------------
-
- void HandleMouseDown (void)
- {
- WindowPtr theWindow;
- short windowCode = FindWindow (gTheEvent.where, &theWindow);
-
- // Where was the mouse down?
- switch (windowCode)
- {
- case inSysWindow:
- {
- SystemClick (&gTheEvent, theWindow);
- break;
- }
- case inMenuBar:
- {
- AdjustMenus();
- HandleMenu (0L);
- break;
- }
- case inDrag:
- {
- DragWindow (theWindow, gTheEvent.where, &(qd.screenBits.bounds));
- break;
- }
- case inGoAway:
- {
- if ((theWindow == gStatusDialog) &&
- TrackGoAway (gStatusDialog, gTheEvent.where))
- {
- DisposDialog (gStatusDialog);
- gStatusDialog = nil;
- }
- break;
- }
- default:
- {
- break;
- }
- }
- }
-
- //-----------------------------------------------------------------------
-
- void AdjustMenus (void)
- {
- register WindowPeek wp = nil;
- short kind = 0;
- Boolean DA = false;
- ComponentDescription compDesc;
- ComponentResult result = noErr;
-
- // What kind of window is frontmost?
- wp = (WindowPeek) FrontWindow();
- kind = wp ? wp->windowKind : 0;
- DA = kind < 0;
-
- // Set our menu item states appropriately
-
- // Apple menu
- Enable ((Handle) gAppleMenu, kAboutItem, true);
-
- // File menu
- Enable ((Handle) gFileMenu, kOpenItem, (gStatusDialog == nil) ? true : false);
- Enable ((Handle) gFileMenu, kCloseItem, DA || ((gStatusDialog != nil) ? true : false));
-
- #ifdef DEBUG_IT
- Enable ((Handle) gFileMenu, kOpenMathComponentItem, true);
- #endif DEBUG_IT
-
- Enable ((Handle) gFileMenu, kOpenMoMathComponentItem, true);
- Enable ((Handle) gFileMenu, kOpenNuMathComponentItem, true);
- Enable ((Handle) gFileMenu, kCloseComponentItem, (gCurrentInstance != 0L) ? true : false);
- Enable ((Handle) gFileMenu, kQuitItem, true);
-
- // Edit menu
- Enable ((Handle) gEditMenu, 1, DA);
- Enable ((Handle) gEditMenu, 3, DA);
- Enable ((Handle) gEditMenu, 4, DA);
- Enable ((Handle) gEditMenu, 5, DA);
- Enable ((Handle) gEditMenu, 6, DA);
-
- // Test menu
- Enable ((Handle) gTestMenu, kCanDoItem, (gCurrentInstance != 0L) ? true : false);
- Enable ((Handle) gTestMenu, kGetVersionItem, (gCurrentInstance != 0L) ? true : false);
- Enable ((Handle) gTestMenu, kGetNameItem, (gCurrentInstance != 0L) ? true : false);
- Enable ((Handle) gTestMenu, kGetInfoItem, (gCurrentInstance != 0L) ? true : false);
- Enable ((Handle) gTestMenu, kGetRefconItem, (gCurrentInstance != 0L) ? true : false);
- Enable ((Handle) gTestMenu, kGetCountItem, (gCurrentInstance != 0L) ? true : false);
- Enable ((Handle) gTestMenu, kGetA5Item, (gCurrentInstance != 0L) ? true : false);
- Enable ((Handle) gTestMenu, kDoDivideItem, (gCurrentInstance != 0L) ? true : false);
- Enable ((Handle) gTestMenu, kDoMultiplyItem, (gCurrentInstance != 0L) ? true : false);
-
- // Only enable add stuff if a MoMath component is around
- if (gCurrentInstance != 0L)
- {
- result = GetComponentInfo ((Component)(gCurrentInstance), &compDesc, nil, nil, nil);
- if (compDesc.componentManufacturer == moMathComponentMfg)
- {
- Enable ((Handle) gTestMenu, kDoAddItem, true);
- }
- else
- {
- Enable ((Handle) gTestMenu, kDoAddItem, false);
- }
- }
- else
- {
- Enable ((Handle) gTestMenu, kDoAddItem, false);
- }
-
- // Figure out which item is checked in the Instance menu
- if (CountMItems (gInstanceMenu) > 0)
- {
- short i;
- short j;
- long tempInst;
- Str255 menuItemStr;
- Str255 tempInstStr;
-
- // Enable the menu
- EnableItem (gInstanceMenu, 0);
-
- // Examine each item
- for (i = 1; i <= CountMItems (gInstanceMenu); i++)
- {
- Boolean foundKey = false;
-
- // Uncheck the item
- CheckItem (gInstanceMenu, i, false);
-
- // Get the item string
- GetItem (gInstanceMenu, i, menuItemStr);
-
- // Extract the instance number from the menu string
- GetItem (gInstanceMenu, i, menuItemStr);
- for (j = 1; j <= menuItemStr[0]; j++)
- {
- if ((gKeyPhrase[1] == menuItemStr[j]) &&
- (gKeyPhrase[2] == menuItemStr[j + 1]) &&
- (gKeyPhrase[3] == menuItemStr[j + 2]) &&
- (gKeyPhrase[4] == menuItemStr[j + 3]) &&
- (gKeyPhrase[5] == menuItemStr[j + 4]))
- {
- foundKey = true;
- break;
- }
- }
-
- // Did we find the phrase key?
- if (foundKey)
- {
- // Copy the instance string
- BlockMove ((Ptr)&menuItemStr[j + 5], (Ptr)&tempInstStr[1],
- menuItemStr[menuItemStr[0] - (j + 4)]);
- tempInstStr[0] = menuItemStr[0] - (j + 4);
-
- // Is it the current instance?
- HexStrToDecimalNumber ((StringPtr)&tempInstStr, &tempInst);
- if (tempInst == (long)(gCurrentInstance))
- {
- // Check it
- CheckItem (gInstanceMenu, i, true);
- }
- }
- }
- }
- else // No instances are currently open, so dim this menu
- {
- DisableItem (gInstanceMenu, 0);
- }
- DrawMenuBar();
- }
-
- //-----------------------------------------------------------------------
-
- void Enable (Handle menu,
- short item,
- Boolean ok)
- {
- // Utility routine to enable and disable menu items
- if (ok)
- {
- EnableItem ((MenuHandle) menu, item);
- }
- else
- {
- DisableItem ((MenuHandle) menu, item);
- }
- }
-
- //-----------------------------------------------------------------------
-
- void HandleMenu (long theMenu)
- {
- long mSelect;
- short menuID;
- short menuItem;
- ComponentResult result = noErr;
- Str255 menuItemStr;
- Str255 tempStr;
- short i;
- short j;
- ComponentDescription mathDesc;
-
- // Did we get a menu?
- if (theMenu == 0L)
- {
- // Nope, get it from menu select
- mSelect = MenuSelect (gTheEvent.where);
- }
- else
- {
- // Yep, use it
- mSelect = theMenu;
- }
-
- // Decode it
- menuID = HiWord (mSelect);
- menuItem = LoWord (mSelect);
-
- // Which menu is it?
- switch (menuID)
- {
- case kAppleID:
- {
- if (menuItem == kAboutItem)
- {
- DoAboutDialog();
- }
- else // It's a DA
- {
- Str255 name;
-
- // Open the DA
- GetPort (&gSavedPort);
- GetItem (gAppleMenu, menuItem, name);
- OpenDeskAcc (name);
- SetPort (gSavedPort);
- }
- break;
- }
- case kFileID:
- {
- switch (menuItem)
- {
- case kOpenItem:
- {
- // Open the status dialog
- OpenStatus();
- break;
- }
- case kCloseItem:
- {
- WindowPeek frontWindow = (WindowPeek) FrontWindow();
-
- // Is a window up?
- if (frontWindow == 0L)
- {
- break;
- }
- else // Yep
- {
- // What kind of window is it?
- if (frontWindow->windowKind < 0)
- {
- // It's a DA
- CloseDeskAcc (frontWindow->windowKind);
- }
- else
- {
- // Is it the status dialog?
- if (gStatusDialog != nil)
- {
- // Close it
- DisposDialog (gStatusDialog);
- gStatusDialog = nil;
- }
-
- // Update the menus
- AdjustMenus();
- }
- }
- break;
- }
-
- #ifdef DEBUG_IT
- case kOpenMathComponentItem:
- {
- OpenMathComponent (kMathComponent);
- break;
- }
- #endif DEBUG_IT
-
- case kOpenMoMathComponentItem:
- {
- OpenMathComponent (kMoMathComponent);
- break;
- }
-
- case kOpenNuMathComponentItem:
- {
- OpenMathComponent (kNuMathComponent);
- break;
- }
-
- case kCloseComponentItem:
- {
- if (gCurrentInstance != 0L)
- {
- // Close the component instance
- result = CloseComponent (gCurrentInstance);
- if (result != noErr)
- {
- GetIndString (gStatusStr, kStatusStrsID, kErrorClosingInstStr);
- }
- else
- {
- GetIndString (gStatusStr, kStatusStrsID, kClosedOKStr);
- }
- UpdateStatus();
-
- // Delete this instance from the instance menu
- DecimalNumberToHexStr ((long) gCurrentInstance, (StringPtr)&tempStr);
- for (i = 1; i <= CountMItems (gInstanceMenu); i++)
- {
- Str255 tempInstStr;
- Boolean foundKey = false;
-
- // Extract the instance number from the menu item string
- GetItem (gInstanceMenu, i, menuItemStr);
- for (j = 1; j <= menuItemStr[0]; j++)
- {
- if ((gKeyPhrase[1] == menuItemStr[j]) &&
- (gKeyPhrase[2] == menuItemStr[j + 1]) &&
- (gKeyPhrase[3] == menuItemStr[j + 2]) &&
- (gKeyPhrase[4] == menuItemStr[j + 3]) &&
- (gKeyPhrase[5] == menuItemStr[j + 4]))
- {
- foundKey = true;
- break;
- }
- }
-
- // Did we find the phrase key?
- if (foundKey)
- {
- // Copy the instance string
- BlockMove ((Ptr)&menuItemStr[j + 5], (Ptr)&tempInstStr[1],
- menuItemStr[menuItemStr[0] - (j + 4)]);
- tempInstStr[0] = menuItemStr[0] - (j + 4);
-
- // Is this the instance we're closing?
- if (RelString (tempInstStr, tempStr, true, true) == 0)
- {
- // Delete this item from the menu
- DelMenuItem (gInstanceMenu, i);
- break;
- }
- }
- }
-
- // Clear out its slot
- for (i = 0; i < kMaxInstances; i++)
- {
- if (gCurrentInstance == gComponentInstance[i])
- {
- gComponentInstance[i] = 0L;
- break;
- }
- }
- gCurrentInstance = 0L;
-
- // Set up on another instance in the menu (if there is one)
- for (i = 0; i < kMaxInstances; i++)
- {
- if (gComponentInstance[i] != 0L)
- {
- gCurrentInstance = gComponentInstance[i];
- break;
- }
- }
- }
-
- // Update the menus
- AdjustMenus();
- break;
- }
- case kQuitItem:
- {
- // We're outta here
- DoQuit();
- break;
- }
- default:
- {
- break;
- }
- }
- break;
- }
- case kEditID:
- {
- if (!SystemEdit (menuItem - 1))
- {
- // We don't really do anything here - feel free to implement something
- // yourself if you want
- SysBeep(5);
- }
- break;
- }
- case kTestID:
- {
- Str255 tempStr;
-
- switch (menuItem)
- {
- case kGetVersionItem:
- {
- long version = GetComponentVersion (gCurrentInstance);
-
- tempStr[0] = 0;
- GetIndString (gStatusStr, kStatusStrsID, kCompVersStr);
- NumToString ((version >> 16), tempStr);
- BlockMove ((Ptr)&tempStr[1], (Ptr)&gStatusStr[gStatusStr[0] + 1],
- tempStr[0]);
- gStatusStr[0] += tempStr[0];
- GetIndString (tempStr, kStatusStrsID, kInterfaceVersStr);
- BlockMove ((Ptr)&tempStr[1], (Ptr)&gStatusStr[gStatusStr[0] + 1],
- tempStr[0]);
- gStatusStr[0] += tempStr[0];
- NumToString ((version & 0x0000FFFF), tempStr);
- BlockMove ((Ptr)&tempStr[1], (Ptr)&gStatusStr[gStatusStr[0] + 1],
- tempStr[0]);
- gStatusStr[0] += tempStr[0] + 1;
- gStatusStr[gStatusStr[0]] = '.';
- UpdateStatus();
- break;
- }
- case kGetNameItem:
- {
- Handle nameHdl = NewHandle (sizeof (Str255));
-
- if (nameHdl != nil)
- {
- result = GetComponentInfo ((Component) gCurrentInstance,
- nil, nameHdl, nil, nil);
- HLock (nameHdl);
- if ((**nameHdl) == 0)
- {
- GetIndString (gStatusStr, kStatusStrsID, kNoNameStr);
- }
- else
- {
- GetIndString (gStatusStr, kStatusStrsID, kNameStr);
- BlockMove ((Ptr)&((**((Str255**)(nameHdl)))[1]),
- (Ptr)&gStatusStr[gStatusStr[0] + 1],
- (**((Str255**)(nameHdl)))[0]);
- gStatusStr[0] += (**((Str255**)(nameHdl)))[0] + 2;
- gStatusStr[gStatusStr[0] - 1] = '”';
- gStatusStr[gStatusStr[0]] = '.';
- }
- HUnlock (nameHdl);
- DisposHandle (nameHdl);
- }
- UpdateStatus();
- break;
- }
- case kGetInfoItem:
- {
- Handle infoHdl = NewHandle (sizeof (Str255));
-
- if (infoHdl != nil)
- {
- result = GetComponentInfo ((Component) gCurrentInstance,
- nil, nil, infoHdl, nil);
- HLock (infoHdl);
- if ((**((Str255**)(infoHdl)))[0] == 0)
- {
- GetIndString (gStatusStr, kStatusStrsID, kNoInfoStr);
- }
- else
- {
- GetIndString (gStatusStr, kStatusStrsID, kInfoStr);
- BlockMove ((Ptr)&((**((Str255**)(infoHdl)))[1]),
- (Ptr)&gStatusStr[gStatusStr[0] + 1],
- (**((Str255**)(infoHdl)))[0]);
- gStatusStr[0] += (**((Str255**)(infoHdl)))[0] + 2;
- gStatusStr[gStatusStr[0] - 1] = '”';
- gStatusStr[gStatusStr[0]] = '.';
- }
- HUnlock (infoHdl);
- DisposHandle (infoHdl);
- }
- UpdateStatus();
- break;
- }
- case kGetRefconItem:
- {
- long refcon = GetComponentInstanceA5 (gCurrentInstance);
-
- NumToString (refcon, tempStr);
- GetIndString (gStatusStr, kStatusStrsID, kRefconStr);
- BlockMove ((Ptr)&tempStr[1], (Ptr)&gStatusStr[gStatusStr[0] + 1],
- tempStr[0]);
- gStatusStr[0] += tempStr[0] + 1;
- gStatusStr[gStatusStr[0]] = '.';
- UpdateStatus();
- break;
- }
- case kGetCountItem:
- {
- long instCount = CountComponentInstances ((Component) gCurrentInstance);
-
- NumToString (instCount, tempStr);
- if (instCount == 1)
- {
- GetIndString (gStatusStr, kStatusStrsID, kThereIsStr);
- BlockMove ((Ptr)&tempStr[1], (Ptr)&gStatusStr[gStatusStr[0] + 1],
- tempStr[0]);
- gStatusStr[0] += tempStr[0];
- GetIndString (tempStr, kStatusStrsID, kInstanceStr);
- BlockMove ((Ptr)&tempStr[1], (Ptr)&gStatusStr[gStatusStr[0] + 1],
- tempStr[0]);
- }
- else
- {
- GetIndString (gStatusStr, kStatusStrsID, kThereAreStr);
- BlockMove ((Ptr)&tempStr[1], (Ptr)&gStatusStr[gStatusStr[0] + 1],
- tempStr[0]);
- gStatusStr[0] += tempStr[0];
- GetIndString (tempStr, kStatusStrsID, kInstancesStr);
- BlockMove ((Ptr)&tempStr[1], (Ptr)&gStatusStr[gStatusStr[0] + 1],
- tempStr[0]);
- }
- gStatusStr[0] += tempStr[0];
- UpdateStatus();
- break;
- }
- case kGetA5Item:
- {
- long compA5 = GetComponentInstanceA5 (gCurrentInstance);
-
- NumToString (compA5, tempStr);
- GetIndString (gStatusStr, kStatusStrsID, kA5Str);
- BlockMove ((Ptr)&tempStr[1], (Ptr)&gStatusStr[gStatusStr[0] + 1],
- tempStr[0]);
- gStatusStr[0] += tempStr[0] + 1;
- gStatusStr[gStatusStr[0]] = '.';
- UpdateStatus();
- break;
- }
- case kDoDivideItem:
- {
- short numerator;
- short denominator;
- short quotient;
-
- DoOperationDialog (&numerator, &denominator);
- result = DoDivide (gCurrentInstance, numerator, denominator,
- "ient);
- NumToString ((long) quotient, tempStr);
- GetIndString (gStatusStr, kStatusStrsID, kDivideResultStr);
- BlockMove ((Ptr)&tempStr[1], (Ptr)&gStatusStr[gStatusStr[0] + 1],
- tempStr[0]);
- gStatusStr[0] += tempStr[0] + 1;
- gStatusStr[gStatusStr[0]] = '.';
- UpdateStatus();
- break;
- }
- case kDoMultiplyItem:
- {
- short firstNum;
- short secondNum;
- short multiplyResult;
-
- DoOperationDialog (&firstNum, &secondNum);
- result = DoMultiply (gCurrentInstance, firstNum, secondNum,
- &multiplyResult);
- NumToString ((long) multiplyResult, tempStr);
- GetIndString (gStatusStr, kStatusStrsID, kMultiplyResultStr);
- BlockMove ((Ptr)&tempStr[1], (Ptr)&gStatusStr[gStatusStr[0] + 1],
- tempStr[0]);
- gStatusStr[0] += tempStr[0] + 1;
- gStatusStr[gStatusStr[0]] = '.';
- UpdateStatus();
- break;
- }
- case kDoAddItem:
- {
- short firstNum;
- short secondNum;
- short addResult;
-
- DoOperationDialog (&firstNum, &secondNum);
- result = DoAdd (gCurrentInstance, firstNum, secondNum, &addResult);
- NumToString ((long) addResult, tempStr);
- GetIndString (gStatusStr, kStatusStrsID, kAddResultStr);
- BlockMove ((Ptr)&tempStr[1], (Ptr)&gStatusStr[gStatusStr[0] + 1],
- tempStr[0]);
- gStatusStr[0] += tempStr[0] + 1;
- gStatusStr[gStatusStr[0]] = '.';
- UpdateStatus();
- break;
- }
- default:
- {
- break;
- }
- }
- break;
- }
- case kInstanceID:
- {
- Str255 instanceStr;
- Boolean foundKey = false;
-
- // Get the menu item string
- GetItem (gInstanceMenu, menuItem, menuItemStr);
-
- // Find the key phrase
- for (i = 1; i <= menuItemStr[0]; i++)
- {
- if ((gKeyPhrase[1] == menuItemStr[i]) &&
- (gKeyPhrase[2] == menuItemStr[i + 1]) &&
- (gKeyPhrase[3] == menuItemStr[i + 2]) &&
- (gKeyPhrase[4] == menuItemStr[i + 3]) &&
- (gKeyPhrase[5] == menuItemStr[i + 4]))
- {
- // Found the key phrase
- foundKey = true;
- break;
- }
- }
-
- // Did we find the key phrase?
- if (foundKey)
- {
- // Copy the instance string
- BlockMove ((Ptr)&menuItemStr[i + 5], (Ptr)&instanceStr[1],
- menuItemStr[0] - (i + 4));
- instanceStr[0] = menuItemStr[0] - (i + 4);
-
- // Get the instance number from the string
- HexStrToDecimalNumber ((StringPtr)&instanceStr, (long *)&gCurrentInstance);
-
- // Update the menus
- AdjustMenus();
-
- // Get the description for this instance
- result = GetComponentInfo ((Component) gCurrentInstance,
- &mathDesc, nil, nil, nil);
- GetIndString (gStatusStr, kStatusStrsID, kCompDescStr);
- BlockMove ((Ptr)&(mathDesc.componentType),
- (Ptr)&(gStatusStr[gStatusStr[0] + 1]), 4);
- gStatusStr[0] += 4;
- gStatusStr[gStatusStr[0] + 1] = ',';
- gStatusStr[gStatusStr[0] + 2] = ' ';
- gStatusStr[0] += 2;
- BlockMove ((Ptr)&(mathDesc.componentSubType),
- (Ptr)&(gStatusStr[gStatusStr[0] + 1]), 4);
- gStatusStr[0] += 4;
- gStatusStr[gStatusStr[0] + 1] = ',';
- gStatusStr[gStatusStr[0] + 2] = ' ';
- gStatusStr[0] += 2;
- BlockMove ((Ptr)&(mathDesc.componentManufacturer),
- (Ptr)&(gStatusStr[gStatusStr[0] + 1]), 4);
- gStatusStr[0] += 4;
- gStatusStr[gStatusStr[0] + 1] = ']';
- gStatusStr[gStatusStr[0] + 2] = '.';
- gStatusStr[0] += 2;
-
- // Update the status dialog
- UpdateStatus();
- }
- break;
- }
- case kCanDoID:
- {
- switch (menuItem)
- {
- case kCanDoOpenItem:
- {
- if (ComponentFunctionImplemented (gCurrentInstance,
- kComponentOpenSelect))
- {
- GetIndString (gStatusStr, kStatusStrsID, kCanDoOpenStr);
- }
- else
- {
- GetIndString (gStatusStr, kStatusStrsID, kCantDoOpenStr);
- }
- UpdateStatus();
- break;
- }
- case kCanDoCloseItem:
- {
- if (ComponentFunctionImplemented (gCurrentInstance,
- kComponentCloseSelect))
- {
- GetIndString (gStatusStr, kStatusStrsID, kCanDoCloseStr);
- }
- else
- {
- GetIndString (gStatusStr, kStatusStrsID, kCantDoCloseStr);
- }
- UpdateStatus();
- break;
- }
- case kCanDoCanDoItem:
- {
- if (ComponentFunctionImplemented (gCurrentInstance,
- kComponentCanDoSelect))
- {
- GetIndString (gStatusStr, kStatusStrsID, kCanDoCanDoStr);
- }
- else
- {
- GetIndString (gStatusStr, kStatusStrsID, kCantDoCanDoStr);
- }
- UpdateStatus();
- break;
- }
- case kCanDoVersionItem:
- {
- if (ComponentFunctionImplemented (gCurrentInstance,
- kComponentVersionSelect))
- {
- GetIndString (gStatusStr, kStatusStrsID, kCanDoVersionStr);
- }
- else
- {
- GetIndString (gStatusStr, kStatusStrsID, kCantDoVersionStr);
- }
- UpdateStatus();
- break;
- }
- case kCanDoRegisterItem:
- {
- if (ComponentFunctionImplemented (gCurrentInstance,
- kComponentRegisterSelect))
- {
- GetIndString (gStatusStr, kStatusStrsID, kCanDoRegisterStr);
- }
- else
- {
- GetIndString (gStatusStr, kStatusStrsID, kCantDoRegisterStr);
- }
- UpdateStatus();
- break;
- }
- case kCanDoTargetItem:
- {
- if (ComponentFunctionImplemented (gCurrentInstance,
- kComponentTargetSelect))
- {
- GetIndString (gStatusStr, kStatusStrsID, kCanDoTargetStr);
- }
- else
- {
- GetIndString (gStatusStr, kStatusStrsID, kCantDoTargetStr);
- }
- UpdateStatus();
- break;
- }
- case kCanDoDoDivideItem:
- {
- if (ComponentFunctionImplemented (gCurrentInstance,
- kDoDivideSelect))
- {
- GetIndString (gStatusStr, kStatusStrsID, kCanDoDoDivideStr);
- }
- else
- {
- GetIndString (gStatusStr, kStatusStrsID, kCantDoDoDivideStr);
- }
- UpdateStatus();
- break;
- }
- case kCanDoDoMultiplyItem:
- {
- if (ComponentFunctionImplemented (gCurrentInstance,
- kDoMultiplySelect))
- {
- GetIndString (gStatusStr, kStatusStrsID, kCanDoDoMultiplyStr);
- }
- else
- {
- GetIndString (gStatusStr, kStatusStrsID, kCantDoDoMultiplyStr);
- }
- UpdateStatus();
- break;
- }
- case kCanDoDoAddItem:
- {
- if (ComponentFunctionImplemented (gCurrentInstance, kDoAddSelect))
- {
- GetIndString (gStatusStr, kStatusStrsID, kCanDoDoAddStr);
- }
- else
- {
- GetIndString (gStatusStr, kStatusStrsID, kCantDoDoAddStr);
- }
- UpdateStatus();
- break;
- }
- default:
- {
- break;
- }
- }
- break;
- }
- default:
- {
- break;
- }
- }
- }
-
- //-----------------------------------------------------------------------
-
- void OpenStatus (void)
- {
- // Get the dialog
- gStatusDialog = GetNewDialog (kStatusDialogID, nil, (WindowPtr)-1L);
-
- // Did we get it?
- if (gStatusDialog != nil)
- {
- short itemType;
- Handle itemHandle;
-
- GetPort (&gSavedPort);
- SetPort (gStatusDialog);
- TextFont (geneva);
- TextFace (0);
- TextSize (10);
- GetDItem (gStatusDialog, kStatusBox, &itemType, &gStatusText, &gStatusBox);
- InsetRect (&gStatusBox, -2, -2);
- FrameRect (&gStatusBox);
- InsetRect (&gStatusBox, 2, 2);
- EraseRect (&gStatusBox);
- SetIText (gStatusText, gStatusStr);
- }
-
- // Update the menus
- AdjustMenus();
- }
-
- //-----------------------------------------------------------------------
-
- void UpdateStatus (void)
- {
- // If we've got a status dialog, update it
- if (gStatusDialog != nil)
- {
- GetPort (&gSavedPort);
- SetPort (gStatusDialog);
- InvalRect (&gStatusBox);
- SetPort (gSavedPort);
- }
- }
-
- //-----------------------------------------------------------------------
-
- void OpenMathComponent (MathType whichOne)
- {
- short i;
- Str255 tempStr;
- Component mathComponentID;
- short mathTypeStrID;
- Boolean foundSlot = false;
-
- // Which type of component do we want to open?
- switch (whichOne)
- {
- case kMathComponent:
- mathComponentID = gMathComponentID;
- mathTypeStrID = kMathStr;
- break;
- case kMoMathComponent:
- mathComponentID = gMoMathComponentID;
- mathTypeStrID = kMoMathStr;
- break;
- case kNuMathComponent:
- mathComponentID = gNuMathComponentID;
- mathTypeStrID = kNuMathStr;
- break;
- default:
- break;
- }
-
- // Were we asked to open some type of Math component?
- if (mathComponentID != 0L)
- {
- // Find an open slot
- for (i = 0; i < kMaxInstances; i++)
- {
- // Is this slot open?
- if (gComponentInstance[i] == 0L)
- {
- // Open the component
- gComponentInstance[i] =
- (MathComponent) OpenComponent (mathComponentID);
- gCurrentInstance = gComponentInstance[i];
- foundSlot = true;
- break;
- }
- }
- }
- else // We were not asked to find some kind of Math component
- {
- GetIndString (gStatusStr, kStatusStrsID, kDidNotFindCompIDStr);
- }
-
- // There were no open slots (we've opened as many instances as our test
- // program will allow)
- if (!foundSlot)
- {
- GetIndString (gStatusStr, kStatusStrsID, kCantOpenAnyMoreInstStr);
- }
- else if (gCurrentInstance != 0L) // We successfully opened the component
- {
- Str255 mathTypeStr;
-
- GetIndString (mathTypeStr, kStatusStrsID, mathTypeStrID);
- GetIndString (gStatusStr, kStatusStrsID, kFoundCompIDAndInstStr);
- DecimalNumberToHexStr ((long) gCurrentInstance, (StringPtr)&tempStr);
- BlockMove ((Ptr)&tempStr[1], (Ptr)&mathTypeStr[mathTypeStr[0] + 1],
- tempStr[0]);
- mathTypeStr[0] += tempStr[0];
- InsMenuItem (gInstanceMenu, mathTypeStr, 255);
- }
- else // We couldn't open an instance of the component
- {
- GetIndString (gStatusStr, kStatusStrsID, kDidNotGetInstStr);
- }
-
- // Update everything
- UpdateStatus();
- AdjustMenus();
- }
-
- //-----------------------------------------------------------------------
-
- void DoAboutDialog (void)
- {
- short itemHit;
- short itemType;
- Handle itemHandle;
- Rect itemRect;
- DialogPtr aboutDialog = GetNewDialog (kAboutDialogID, nil, (WindowPtr)-1L);
-
- // Do the boring about dialog
- GetDItem (aboutDialog, kOKButtonOutline, &itemType, &itemHandle, &itemRect);
- SetDItem (aboutDialog, kOKButtonOutline, itemType,
- (Handle) DrawOKOutline, &itemRect);
- do
- {
- ModalDialog (nil, &itemHit);
- }
- while (itemHit != kOKButton);
- DisposDialog (aboutDialog);
- }
-
- //-----------------------------------------------------------------------
-
- void DoOperationDialog (short *firstNum,
- short *secondNum)
- {
- DialogPtr opDialog = GetNewDialog (kOpDialogID, nil, (WindowPtr)-1L);
-
- // Did we get a dialog?
- if (opDialog != nil)
- {
- short itemHit;
- short itemType;
- Handle itemHandle;
- Rect itemRect;
- Str255 tempStr;
- long tempNum;
-
- GetDItem (opDialog, kFirstNumBox, &itemType, &itemHandle, &itemRect);
- NumToString (23L, tempStr);
- SetIText (itemHandle, tempStr);
-
- GetDItem (opDialog, kSecondNumBox, &itemType, &itemHandle, &itemRect);
- NumToString (4L, tempStr);
- SetIText (itemHandle, tempStr);
-
- // Go get two numbers from the user
- do
- {
- ModalDialog (nil, &itemHit);
- }
- while (itemHit != kOKButton);
-
- // Give the two numbers back to the caller
- GetDItem (opDialog, kFirstNumBox, &itemType, &itemHandle, &itemRect);
- GetIText (itemHandle, tempStr);
- StringToNum (tempStr, &tempNum);
- *firstNum = (short) tempNum;
-
- GetDItem (opDialog, kSecondNumBox, &itemType, &itemHandle, &itemRect);
- GetIText (itemHandle, tempStr);
- StringToNum (tempStr, &tempNum);
- *secondNum = (short) tempNum;
-
- // Clean up
- DisposDialog (opDialog);
- }
- }
-
- //-----------------------------------------------------------------------
-
- static pascal void DrawOKOutline (DialogPtr theDialog,
- short theItem)
- {
- short itemType;
- Handle itemHandle;
- Rect itemRect;
-
- // Draws the OK button outline
- SetPort (theDialog);
- GetDItem (theDialog, kOKButtonOutline, &itemType, &itemHandle, &itemRect);
- PenSize (3, 3);
- InsetRect (&itemRect, -4, -4);
- FrameRoundRect (&itemRect, 16, 16);
- PenSize (1, 1);
- }
-
- //-----------------------------------------------------------------------
-
- void DoQuit (void)
- {
- ComponentResult result = noErr;
- short i;
-
- // Walk our instance array and close all instances
- for (i = 0; i < kMaxInstances; i++)
- {
- if (gComponentInstance[i] != 0L)
- {
- result = CloseComponent (gComponentInstance[i]);
- gComponentInstance[i] = 0L;
- }
- }
- // Note that it's not necessary to unregister the components
- // or dispose of the string handles for the name and info
- // strings when we're in debug mode - this is because we registered
- // the components locally, and as soon as the application heap
- // shuts down (that is, the app quits), the Component Manager
- // automatically unregisters any components local to that app heap and
- // disposes of the component handles properly.
-
- // Set quit flag
- gQuitFlag = true;
- }
-
- //-----------------------------------------------------------------------
-
- void DecimalNumberToHexStr (long theDecimalNumber,
- StringPtr theHexStr)
- {
- // Utility routine to convert a decimal number into a
- // hex number string
-
- long testNybble;
- char hexStr[8];
- Str255 tempStr;
- short i;
- short j;
-
- for (i = 7; i >= 0; i--)
- {
- testNybble = (theDecimalNumber & (0xF0000000 >> (4 * i))) >> ((7 - i) * 4);
-
- if ((testNybble == 8) || (testNybble == 9)) // is it 8 or 9?
- {
- NumToString (testNybble, tempStr);
- hexStr[i] = tempStr[1];
- }
- else if ((testNybble & 0x00000008) == 1) // is it between A and F?
- {
- if (testNybble == 10)
- hexStr[i] = 'A';
- else if (testNybble == 11)
- hexStr[i] = 'B';
- else if (testNybble == 12)
- hexStr[i] = 'C';
- else if (testNybble == 13)
- hexStr[i] = 'D';
- else if (testNybble == 14)
- hexStr[i] = 'E';
- else if (testNybble == 15)
- hexStr[i] = 'F';
- }
- else // it's between 0 and 7
- {
- NumToString (testNybble, tempStr);
- hexStr[i] = tempStr[1];
- }
- }
- BlockMove ((Ptr)&hexStr, (Ptr)&theHexStr[1], 8);
- theHexStr[0] = 8;
- }
-
- //-----------------------------------------------------------------------
-
- OSErr HexStrToDecimalNumber (StringPtr theHexStr,
- long *theDecimalNumber)
- {
- // Utility routine to convert a hex number string into
- // a decimal number
-
- OSErr result = noErr;
- long decimalNum = 0L;
- short i;
- long hexDigitValue;
- Str255 tempStr;
-
- tempStr[0] = 1;
- if (theHexStr[0] > 8)
- {
- result = kGenericError;
- }
- else
- {
- for (i = 1; i <= 8; i++)
- {
- if (theHexStr[i] >= 0x30 && theHexStr[i] <= 0x39) // it's between 0 and 9
- {
- tempStr[1] = theHexStr[i];
- StringToNum (tempStr, &hexDigitValue);
- decimalNum += hexDigitValue << (4 * (8 - i));
- }
- else if ((theHexStr[i] >= 0x41 && theHexStr[i] <= 0x46) ||
- (theHexStr[i] >= 0x61 && theHexStr[i] <= 0x66)) // it's between A and F
- {
- switch (theHexStr[i])
- {
- case 'A':
- case 'a':
- hexDigitValue = 10;
- break;
- case 'B':
- case 'b':
- hexDigitValue = 11;
- break;
- case 'C':
- case 'c':
- hexDigitValue = 12;
- break;
- case 'D':
- case 'd':
- hexDigitValue = 13;
- break;
- case 'E':
- case 'e':
- hexDigitValue = 14;
- break;
- case 'F':
- case 'f':
- hexDigitValue = 15;
- break;
- }
- decimalNum += hexDigitValue << (4 * (8 - i));
- }
- else
- {
- result = kGenericError;
- }
- }
- }
-
- *theDecimalNumber = decimalNum;
- return (result);
- }
-
- //-----------------------------------------------------------------------
-
-